<?
ini_set('allow_url_fopen', 'on');
if (get_magic_quotes_gpc()) {
    die("Magic qoutes er skrudd på i PHP, dette er dritt. Fiks <em>php_flag magic_quotes_gpc off</em> i .htaccess"); 
}

function curl_read($url) {
    // create a new cURL resource
    $ch = curl_init();
      
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    $out = curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    return $out;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Regex-testbox for musiceye</title>
</head>
<body>
<h2>Regex-testbox for musiceye</h2>
<form method="POST">
<table border="0">
 <tr>
   <td>URL:</td>
   <td><input type="text" size="70" name="url" value="<?=$_POST['url']?>">&nbsp;</td>
   <td rowspan="2"><div style="background-color: #EEEEEE">Søket blir gjort med <code><a href="http://php.net/preg_match_all">preg_match_all()</a></code> og resultatet skal bli
    <br/>
    <code>$matches[0] = array with matched code<br/>
   		  $matches[1] = array with first matches (url)<br/>
   		  $matches[2] = array with second matches (artist)</code><br/>
   		  En nyttig liten guide: <a href="http://www.the-art-of-web.com/php/parse-links/">Parsing HTML to find links</a>
    </div>
   </td>
 </tr>
 <tr>
   <td>Use CURL:</td>
   <td>
   <?
   if($_POST['curl'] == 1) {
       $checked = 'checked="checked"'; 
   }
   ?>
   <input type="checkbox" name="curl" value="1" <?=$checked?>>
   </td>	 
 </tr>
 <tr>
   <td>Pattern: </td>
   <td colspan="2">
    <em>Example:</em> <code><?=htmlspecialchars('<td><strong><a href=\"([^\"]*)\">(.*)<\/a><\/strong><\/td>')?></code><br/>
    <textarea rows="5" cols="75" style="font-size:115%" name="pattern"><?=htmlspecialchars($_POST['pattern'])?></textarea>
   </td>
 </tr>
 <tr>
   <? 
   //<td><input type="text" size="75" name="pattern" style="font-size:115%" value="<?=htmlspecialchars($_POST['pattern'])">
  ?>
 </tr>
</table><input type="submit" value="send" name="submit">
<br/><br/>

<?php
if(!empty($_POST['submit'])) {
    echo "<b>Results for:</b> <code>".htmlspecialchars($_POST['pattern'])."</code><br/><br/>";
    if($_POST['curl'] == 1) {
        $fileContent = curl_read($_POST['url']);
    } else {
        $fileContent = file_get_contents($_POST['url']);
    }
    if(preg_match_all("/".$_POST['pattern']."/siU", $fileContent, $matches)) {
        if(empty($matches) || count($matches) < 3) {
            die("preg_match_all() has failed.");
        } else {
            echo "<pre>";
            var_dump($matches);
            echo "</pre>";
        }
    } else {
        echo "No matches.";
    }
}
?>
</pre>
</form>
</body>
</html>